In this lecture, I want to talk about the powerful logical operators we have in Lua that allow us to
make our conditions more complex.
These logical operators are and, or and not.
Using these operators, we can make condition checking more complex, as we can watch for more than
one condition to be true or false.
So as an example, what I'm going to do is I'm going to make several variables here.
I'm going to call this first variable example num one.
And we can set it equal to a value of ten.
And then I'm going to create a second variable.
We can call this example num two and set it equal to 50.
And then I'm going to create another variable I'll just call this example bool.
And I'm going to set it equal to true.
Then what I'm going to do is I'm going to use an if statement and we're going to check some conditions
here.
So for example, maybe I want to check if example num one is equal to ten right.
If example number one is equal to ten, then I want to execute this block of code here.
But maybe I also want to make sure that example number two is also equal to 50.
This is where we can use the and keyword.
So I'm going to type out and here and then we can pass another condition to check.
So I want to check if example number two is equal to 50.
This is reading out like basic English.
If the value stored in this variable is equal to ten and the value stored in this variable is equal
to 50, then we're going to execute this block of code.
This means that both of these conditions must evaluate to true in order for this block of code to execute.
If either one of these conditions evaluates to false, then it's not going to execute.
So when the interpreter goes and it checks the first condition, if this first condition evaluates to
false, it's going to ignore the second condition and just skip this if statement block.
If the first condition is true, then it's going to check the second condition.
If the second condition is false, then it's going to ignore this block of code.
However, if both are true, then we're going to execute what's inside of here.
So I can print.
The conditions were truthy.
So now if I go ahead and run my game.
As you can see, the conditions were truthy because the value stored in the first variable was ten and
the value stored in the second variable was 50.
Let's go ahead and make this a little bit more complex.
What I want to do is now use the Or keyword, and I want to check a third condition here.
I'm going to pass our example bool here.
And I want to check to see if this is true.
So we can type out if the value in here is equal to true.
Or we can completely omit that.
And it will just check to see if a value exists within our variable or the value inside of there is
truthy.
Now what I'm going to do next is I'm going to change the value inside of example number to equal to
40.
What do you think is going to happen here?
Do you think this block of code is going to execute or not execute?
Well let's see what happens.
We can hit run.
And as you can see, it says the conditions were truthy.
Wait, what?
How were the conditions?
Truthy.
Let's go ahead and take another look again.
So when our interpreter has reached this if statement, what it's going to do is it's going to check
this first condition.
Of course this first condition evaluated to true.
And then it's going to check the second condition.
Well, the second condition is false, right?
So it should have ignored this block of code, right?
Actually it's not going to because we have the Or statement here.
So basically what's happening here is that either example number one and example number two have to
be equal to their respective values.
Or just this single variable right here has to contain a value or evaluate to be true.
Either these two values have to be true right.
Or just this value right here has to be true.
Now maybe you don't want it to be evaluated in this way.
And this is where we can use parentheses in order to change how this if statement is evaluated.
For example, what I'm going to do here is I'm going to wrap this first condition in parentheses.
And then I'm going to wrap these other two conditions and another pair of parentheses.
Now we have changed how this condition is going to be evaluated.
So what's going to happen here is it's going to check this first condition.
If this first condition evaluates to true then that means what's going to happen next is either example
number two has to be equal to 50.
Or example bool has to be truthy.
Because we have inserted these parentheses.
Here we've changed the logic of how this if statement or these conditions are going to be evaluated.
So this has to be true.
And either this has to be true or this has to be true.
So that means if we go ahead and run the game, we're still going to have it printed out into the console
because that last condition evaluated to true.
But if we set the value inside of there equal to false, and then we go and run our game.
As you can see, nothing was printed inside of the console because even though this condition here evaluated
to true, neither this condition nor this condition evaluated to true, so it did not execute this if
statement block.
The last operator we're going to take a look at is the not operator, which is an interesting one.
Why is it interesting?
Because it can negate truthy values and make falsy values true.
So for example, if I had a falsy value and I placed the Not keyword before it, it would now become
truthy because it negated the falsy value.
You can think of it like multiplying a negative number with a negative number to make a positive number,
and this is also the case if you multiply a negative number with a positive number to make that positive
number negative.
So if you put the not keyword behind a truth value, it would become false or evaluate to false.
So as an example here I'm going to create another if statement.
And we're going to use the not keyword.
And what we're going to do is we're going to negate the value stored in our example boolean variable.
So if not example bool then what we're going to do is print out the condition was false but the not
made it truthy.
So basically the value inside of this variable was false.
But because of the not keyword here, we forced this to evaluate to true and execute this block of code.
You can again think of this as basic English.
If this is not true, then we're going to execute this block of code.
And since the example boolean variable stores the value of false which is not true, then this is going
to execute.
So if we go ahead and hit run here as you can see it prints out the condition was false.
But the not made it truthy to continue demonstrating the power of this not operator.
What I'm going to do is I'm going to put a print statement here and I'm going to print out not not nil.
What do you think is going to print out inside of the console.
Well, let's go ahead and hit run and see what happens.
As you can see, it printed out false.
And you'll see why here in a moment.
So what's happening here is not nil is true, right?
We've negated the value of nil and made it evaluate to true.
However, we have a second.
Not here.
And since not nil is true, and then not true is, you guessed it, false.
So we print it out false inside of the console.
Another use case we have for these logical operators are assigning default values to variables.
I'm going to show you what I mean.
I'm going to create a variable.
I'm going to call it this Val, and I'm going to set it equal to nil.
So it has no value inside of it.
Then I'm going to create another variable.
I'm going to call this some var.
And what I'm going to do is I'm going to set it equal to this Val.
But then I'm going to use the Or keyword here and put the value of like ten.
So what's going to happen here is that the value stored inside of some bar is going to be this val.
Or if this val happens to be false or it evaluates to false, then the value of ten is going to be stored
in some var.
And this is because of how conditions work.
Conditions are going to return.
The first value that is truthy, or a condition is going to return the last value when all previous
conditions were false, regardless if the last value is truthy or not.
So as an example, we can demonstrate the power of these logical operators in real time by using the
command line.
If you do not have the command line open, you can head up to the view tab and open the command line
or the command bar right here.
And what I'm going to do inside of the command line is I'm going to print out into the console.
We're going to print out nil or ten.
And when we hit enter, as you can see, what happens is we got ten printed out into the console.
And as I said earlier, this is because conditions will return.
The first value that it finds is truthy, or it's going to return the last value regardless if it is
truthy or not.
And to demonstrate that what I'm going to do is we could pass nil here or we could pass false.
Right?
What I think is going to print nil or false if we hit enter.
As you can see, we got false because nil evaluated to true.
And since false is the last condition being checked here, it got returned by default.
So that's why false printed inside of the console.
If we flip the position of these two values and I put false first or nil, and then we hit enter.
As you can see nil printed instead because this first condition evaluated to false.
And since nil is the last condition being checked, it got returned by default.
And using this behavior, we can create default values for variables in case one of them happens to
be false.
So for example, in any case, you may want a player to define or update their own client sided settings.
So for example you might have a default setting for some particular thing.
And the client can change that setting to their preference.
What we could do is we could check to see if a player has changed a setting to their own personal preference.
If not, we can assign a default value that we've created for all the players to have when they don't
manipulate any settings on their end.
So that means when I go and print some var into the console, we're going to get the value of ten here.
And that's because this value is nil.
It evaluates to false.
So it defaults with the value of ten.
So if we go and run our game here.
As you can see, we got ten printed out inside of our console.
Now if I put a different value in this val.
For example, if I put a string like hello, since the string is true, that means this value is going
to be stored in some var and it's going to ignore ten because like I said, the first value that evaluates
to true is going to be stored or returned from this condition and stored inside of our variable.
So if I hit run, as you can see, we get hello printed into the console.
Logical operators have so many uses in Lua and you can use them in many different places.
So please, I'd really recommend for you to continue messing with logical operators in your scripts.
Take a few minutes using the command line down here to get a good understanding of how these logical
operators work.
After you do that, I will see you in the next lecture.